import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class MapReader 
{
	static final int tileWidth = 32;
	static final int tileHeight = 32;
	public static HashMap<BlockLocation, Entity> readMap(Game game, String loc)
	{
		HashMap<BlockLocation, Entity> tileMap = new HashMap<BlockLocation, Entity>();
		Entity tile;
		try
		{
			int x = 0;
			int y = 0;
			Scanner lineScanner = new Scanner(new File(loc));
			String line = "";
			Scanner tokenScanner = new Scanner(line);
			String token = "";
			while (lineScanner.hasNextLine())
			{
				line = lineScanner.nextLine();
				tokenScanner = new Scanner(line);
				while (tokenScanner.hasNext())
				{
					token = tokenScanner.next();
					token.trim();
					tile = getTileType(token, game, x, y);
					tileMap.put(new BlockLocation(x,y), tile);
					x += tileWidth;
				}
				x = 0;
				y += tileHeight;
			}
			System.out.println("Number of tiles:" + tileMap.size());
			return tileMap;
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		return null;
	}
	public static Entity getTileType(String s, Game game, int x, int y)
	{
		System.out.println(s);
		if (s.equals("_"))
		{
			return getGroundTile(game, x, y);
		}
		else if (s.equals("*"))
		{
			return getWaterTile(game, x, y);
		}
		else if (s.equals("^"))
		{
			return getIcicle(game, x, y);
		}
		else if (s.equals("%"))
		{
			return getGlassTile(game, x, y);
		}
		else if (s.equals("W"))
		{
			return getWaterTopTile(game, x, y);
		}
		else if (s.equals("G"))
		{
			return new GroundTile(game, x, y, game.getStorage().getSprite("res//sss.png", 1));
		}
		else if (s.equals("E"))
		{
			game.objects.add(new Elevator(game, x, y, game.getStorage().getSprite("res//platform.png", 1)));
			return null;
		}
		else if (s.equals("."))
		{
			return null;
		}
		return null;
	}
	
	public static WaterTile getWaterTile(Game game, int x, int y)
	{
		int randomNum = (int)(Math.random() * 5);
		switch(randomNum)
		{
			case 0: return new WaterTile(game, x, y, game.getStorage().getSprite("res//water1.png", 1));
			case 1: return new WaterTile(game, x, y, game.getStorage().getSprite("res//water2.png", 1));
			case 2: return new WaterTile(game, x, y, game.getStorage().getSprite("res//water3.png", 1));
			case 3: return new WaterTile(game, x, y, game.getStorage().getSprite("res//water4.png", 1));
			case 4: return new WaterTile(game, x, y, game.getStorage().getSprite("res//water5.png", 1));
			default: return new WaterTile(game, x, y, game.getStorage().getSprite("res//water1.png", 1));
		}
	}
	public static WaterTile getWaterTopTile(Game game, int x, int y)
	{
		int randomNum = (int)(Math.random() * 3);
		switch(randomNum)
		{
			case 0: return new WaterTile(game, x, y, game.getStorage().getSprite("res//waterT1.png", 1));
			case 1: return new WaterTile(game, x, y, game.getStorage().getSprite("res//waterT2.png", 1));
			case 2: return new WaterTile(game, x, y, game.getStorage().getSprite("res//waterT3.png", 1));
			default: return new WaterTile(game, x, y, game.getStorage().getSprite("res//waterT1.png", 1));
		}
	}
	public static GroundTile getGroundTile(Game game, int x, int y)
	{
		int randomNum = (int)(Math.random() * 4);
		switch(randomNum)
		{
			case 0: return new GroundTile(game, x, y, game.getStorage().getSprite("res//GroundUnder 1.png", 1));
			case 1: return new GroundTile(game, x, y, game.getStorage().getSprite("res//GroundUnder 2.png", 1));
			case 2: return new GroundTile(game, x, y, game.getStorage().getSprite("res//GroundUnder 3.png", 1));
			case 3: return new GroundTile(game, x, y, game.getStorage().getSprite("res//GroundUnder 4.png", 1));
			default: return new GroundTile(game, x, y, game.getStorage().getSprite("res//GroundUnder 1.png", 1));
		}
	}
	public static GroundTile getGlassTile(Game game, int x, int y)
	{
		int randomNum = (int)(Math.random() * 4);
		switch(randomNum)
		{
		case 0: return new GroundTile(game, x, y, game.getStorage().getSprite("res//ice.png", 1));
		case 1: return new GroundTile(game, x, y, game.getStorage().getSprite("res//ice2.png", 1));
		case 2: return new GroundTile(game, x, y, game.getStorage().getSprite("res//ice3.png", 1));
		case 3: return new GroundTile(game, x, y, game.getStorage().getSprite("res//ice4.png", 1));
		default: return new GroundTile(game, x, y, game.getStorage().getSprite("res//ice.png", 1));
		}
	}
	public static IceTile getIceTile(Game game, int x, int y)
	{
		int randomNum = (int)(Math.random() * 4);
		switch(randomNum)
		{
			case 0: return new IceTile(game, x, y, game.getStorage().getSprite("res//ice.png", 1));
			case 1: return new IceTile(game, x, y, game.getStorage().getSprite("res//ice2.png", 1));
			case 2: return new IceTile(game, x, y, game.getStorage().getSprite("res//ice3.png", 1));
			case 3: return new IceTile(game, x, y, game.getStorage().getSprite("res//ice4.png", 1));
			default: return new IceTile(game, x, y, game.getStorage().getSprite("res//ice.png", 1));
		}
	}
	public static Icicle getIcicle(Game game, int x, int y)
	{
		int randomNum = 4;
		switch(randomNum)
		{
			case 0: return new Icicle(game, x, y, game.getStorage().getSprite("res//Icicle1.png", 1));
			case 1: return new Icicle(game, x, y, game.getStorage().getSprite("res//Icicle2.png", 1));
			case 2: return new Icicle(game, x, y, game.getStorage().getSprite("res//Icicle3.png", 1));
			case 3: return new Icicle(game, x, y, game.getStorage().getSprite("res//Icicle5.png", 1));
			case 4: return new Icicle(game, x, y, game.getStorage().getSprite("res//IcicleTest.png", 1));			
			default: return new Icicle(game, x, y, game.getStorage().getSprite("res//Icicle1.png", 1));
		}
	}
	
}